package com.lateam2010.register.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public final class Register extends Composite {
	private static RegisterUiBinder uiBinder = GWT.create(RegisterUiBinder.class);

	interface RegisterUiBinder extends UiBinder<Widget, Register> {
	}

	private final RegisterServiceAsync service = GWT.create(RegisterService.class);

	@UiField Label error;
	@UiField TextBox password;
	@UiField TextBox newPassword;
	@UiField Panel list;
	@UiField Button update;
	@UiField Button add;
	@UiField TextBox email;
	@UiField TextBox name;
	@UiField Button changePassword;
	
	public Register() {
		initWidget(uiBinder.createAndBindUi(this));
		error.setVisible(false);
		
		String cp = Cookies.getCookie("password");
		if (cp != null) {
			password.setText(cp);
		}
		
		update.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				error.setVisible(false);
				update.setVisible(false);
				service.list(password.getText(), new AsyncCallback<String[][]>() {
					@Override
					public void onFailure(Throwable caught) {
						error.setVisible(true);
						update.setVisible(true);
					}
					@Override
					public void onSuccess(String[][] result) {
						setCookies();
						list.clear();
						update.setVisible(true);
						for (String[] a : result) {
							list.add(new Item(password, error, service, a[0], a[1]));
						}
					}
				});
			}
		});

		add.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				error.setVisible(false);
				add.setVisible(false);
				final String newEmail = email.getText();
				final String newName = name.getText();
				service.register(password.getText(), newEmail, newName, new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						error.setVisible(true);
						add.setVisible(true);
					}
					@Override
					public void onSuccess(Void result) {
						setCookies();
						list.add(new Item(password, error, service, newEmail, newName));
						add.setVisible(true);
					}
				});
			}
		});

		changePassword.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				error.setVisible(false);
				changePassword.setVisible(false);
				final String p = newPassword.getText();
				service.changePassword(password.getText(), p, new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						error.setVisible(true);
						changePassword.setVisible(true);
					}
					@Override
					public void onSuccess(Void result) {
						password.setText(p);
						setCookies();
						changePassword.setVisible(true);
					}
				});
			}
		});
	}

	private void setCookies() {
		Cookies.setCookie("password", password.getText());
	}
	
}
